package com.pig4cloud.mall.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.mall.product.entity.SkuStock;
import com.pig4cloud.mall.product.service.SkuStockService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * sku的库存
 *
 * @author fangyu
 * @date 2020-12-26 23:41:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/skustock")
@Api(value = "skustock" , tags = "sku的库存管理")
public class SkuStockController {

    private final  SkuStockService skuStockService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param skuStock sku的库存
     * @return
     */
    @ApiOperation(value = "分页查询" , notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('product_skustock_get')")
    public R getSkuStockPage(Page page, SkuStock skuStock) {
        return R.ok(skuStockService.page(page, Wrappers.query(skuStock)));
    }


    /**
     * 通过id查询sku的库存
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询" , notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('product_skustock_get')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(skuStockService.getById(id));
    }

    /**
     * 新增sku的库存
     * @param skuStock sku的库存
     * @return R
     */
    @ApiOperation(value = "新增sku的库存" , notes = "新增sku的库存")
    @SysLog("新增sku的库存")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('product_skustock_add')")
    public R save(@RequestBody SkuStock skuStock) {
        return R.ok(skuStockService.save(skuStock));
    }

    /**
     * 修改sku的库存
     * @param skuStock sku的库存
     * @return R
     */
    @ApiOperation(value = "修改sku的库存" , notes = "修改sku的库存")
    @SysLog("修改sku的库存")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('product_skustock_edit')")
    public R updateById(@RequestBody SkuStock skuStock) {
        return R.ok(skuStockService.updateById(skuStock));
    }

    /**
     * 通过id删除sku的库存
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除sku的库存" , notes = "通过id删除sku的库存")
    @SysLog("通过id删除sku的库存")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('product_skustock_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(skuStockService.removeById(id));
    }

}
